package Impl;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import jobFactoryItf.CallBackHelper;
import jobFactoryItf.JobFactory;
import jobFactoryItf.JobFactoryHelper;
import jobFactoryItf.JobHelper;
import jobFactoryItf.JobHolder;

public class Client {
	static JobFactory href;
	
	public static void main(String args[]) throws IOException {
		ORB orb = ORB.init(args, null);
		
		// new ThreadRun(orb).start();
		
		POA rootpoa = null;
		try {
			rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			try {
				rootpoa.the_POAManager().activate();
			} catch (AdapterInactive e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (InvalidName e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("FicPourIOR"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String ior = null;
		
		ior = br.readLine();
		
		br.close();
		
		Object obj = orb.string_to_object(ior);
		href = JobFactoryHelper.narrow(obj);
		
		JobHolder h = new JobHolder();
		for (int i = 0; i < 6; i++) {
			
			CallBackImpl c = new CallBackImpl();
			
			org.omg.CORBA.Object ref = null;
			
			try {
				ref = rootpoa.servant_to_reference(c);
			} catch (ServantNotActive | WrongPolicy e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			href.creer_job((short) i, h, CallBackHelper.narrow(ref));
			h.value.faireJob();
		}
		orb.run();
	}	
}